/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.util.Vector;
import model.entity.*;

public class CategoryImp {

   
    public List<Category> ListCategory(int parentid)
    {
         
        List<Category> lcat = new ArrayList<Category> ();
        try
        {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from category where parentid = "+ parentid);
            while(rs.next())
            {
                lcat.add(new Category(rs.getInt("categoryid"),rs.getString("categoryname"),rs.getInt("parentid")));
            }
        }
        catch(Exception ex)
        {
            lcat = null;
        }
        return lcat;
    }
    public boolean EditCategory(int categoryid,String categoryname,int parentid)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("update category set categoryname = ?, parentid = ? where categoryid = ?");
            prst.setString(1, categoryname);
            prst.setInt(2, parentid);
            prst.setInt(3, categoryid);
            prst.execute();
            prst.close();
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    public boolean DeleteCategory(int categoryid)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("delete from category where categoryid = ?");
            prst.setInt(1, categoryid);
            prst.execute();
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    public boolean AddCategory(String categoryname,int parentid)
    {
        boolean kq = true;
        try {
            
        }
        catch (Exception e) {
        }
        return kq;
    }
    
    public List<Category> ChildCategory()
    {
         
        List<Category> lcat = new ArrayList<Category> ();
        try
        {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from category where parentid <> 0");
            while(rs.next())
            {
                lcat.add(new Category(rs.getInt("categoryid"),rs.getString("categoryname"),rs.getInt("parentid")));
            }
        }
        catch(Exception ex)
        {
            lcat = null;
        }
        return lcat;
    }
    
    
    public List<Category> ParentCategory()
    {
        List<Category> lcat = new ArrayList<Category> ();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from category where parentid = 0");
            while(rs.next())
            {
                lcat.add(new Category(rs.getInt("categoryid"),rs.getString("categoryname"),rs.getInt("parentid")));
            }
        } catch (Exception e) {
            lcat = null;
        }
        return lcat;
    }
    public boolean CreateCategory(String categoryname,int parentid)
    {
        boolean kq = true;
        try {
            Connection con  = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("insert into category values (? ,?)");
            prst.setString(1, categoryname);
            prst.setInt(2, parentid);
            prst.execute();
            prst.close();
        }
        catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    
    public Category DetailCategory(int categoryid)
    {
        Category cat = new Category();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from category where categoryid = " + categoryid);
            while(rs.next())
            {
                cat = new Category(rs.getInt("categoryid"), rs.getString("categoryname"), rs.getInt("parentid"));
            }
        } catch (Exception e) {
            cat = null;
        }
        return cat;
    }
    
    
}
